package com.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;

import java.util.ArrayList;

@EnableWebSecurity(debug = true)
@Configuration
public class Config extends WebSecurityConfigurerAdapter {


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable().
                authorizeRequests()
                .antMatchers("/manage/**").hasAnyAuthority("ROLE_ACTIVITI_USER","ROLE_ACTIVITI_ADMIN","GROUP_management")
                .anyRequest().authenticated()
                .and()
                .formLogin().permitAll() // permit for login page.
                .usernameParameter("username").passwordParameter("password");
    }

    @Bean
    public UserDetailsService userDetailsService() {
        ArrayList<UserDetails> users = new ArrayList<>();
        users.add(User.withUsername("user").password("{bcrypt}$2a$10$Nl8zvMdhS0l22hBR.hAsNuQeMWH0.dgVRC/4.a0ny37SiI2Hbc4iW").authorities("ROLE_ACTIVITI_USER", "GROUP_management").build());
         users.add(User.withUsername("joe").password("{bcrypt}$2a$10$Nl8zvMdhS0l22hBR.hAsNuQeMWH0.dgVRC/4.a0ny37SiI2Hbc4iW").authorities("ROLE_ACTIVITI_USER",  "GROUP_management").build());
        users.add(User.withUsername("jim").password("{bcrypt}$2a$10$Nl8zvMdhS0l22hBR.hAsNuQeMWH0.dgVRC/4.a0ny37SiI2Hbc4iW").authorities("ROLE_ACTIVITI_USER",  "GROUP_management").build());
          users.add(User.withUsername("john").password("{bcrypt}$2a$10$Nl8zvMdhS0l22hBR.hAsNuQeMWH0.dgVRC/4.a0ny37SiI2Hbc4iW").authorities( "ROLE_ACTIVITI_USER", "GROUP_management").build());
        users.add(User.withUsername("system").password("{bcrypt}$2a$10$6r4WkpfuAZ/k7UnoOwtXo.QsMTO.nBz2wZKtwG63t3VcFQj1BXxqC").roles("ACTIVITI_USER").build());
        users.add(User.withUsername("admin").password("{bcrypt}$2a$10$6r4WkpfuAZ/k7UnoOwtXo.QsMTO.nBz2wZKtwG63t3VcFQj1BXxqC").roles("ACTIVITI_ADMIN").build());
        return new InMemoryUserDetailsManager(users);
    }
}